/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Jury;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Pierre
 */
public class JuryDAO extends DAO<Jury> {

    public JuryDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Jury obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Jury obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Jury obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int next() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public Jury find(int id) {
        Jury jury = new Jury();

        try {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "select * from jury WHERE juryid = " + id);

            if (res.first()) {
                jury = new Jury(id, res.getString("categorie"));

                JureDAO jureDao = new JureDAO(this.connect);


                res = this.connect.createStatement().executeQuery(
                    "SELECT jureid FROM jure WHERE juryid = " + id);

                while (res.next()) {
                    jury.addJure(jureDao.find(res.getInt("jureid")));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jury;

    }

    @Override
    public ArrayList<Jury> findAll() {
        ArrayList<Jury> listeJury = new ArrayList<Jury>();
        Jury jury;
        try {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "select * from jury ");
            
            JureDAO jureDao = new JureDAO(this.connect);
            ResultSet resJure;
            
            while(res.next()) {
                jury = new Jury(res.getInt("juryid"), res.getString("categorie"));
                
                System.out.println(res.isLast());

                resJure = this.connect.createStatement().executeQuery(
                    "SELECT jureid FROM jure WHERE juryid = " + jury.getId());

                while (resJure.next()) {
                    jury.addJure(jureDao.find(resJure.getInt("jureid")));
                }
                
                listeJury.add(jury);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeJury;
        
    }
}
